/**
*本文件编写基本用例编辑和序列编辑页面中公共方法
*
**/
//调取列车类型获取接口更新列车类型下拉内容以及车种标识联动更新下拉选项
//id:文件id
//trainType:列车类型值
//trainBZ:车种标识值
function getLKJTrainType(id,trainType,trainBZ){
	var result = [];
	$.ajax({
		url : '../../../basecase/getTrainType',
		type : "POST",
		data :  {fileId: id},
		success : function(obj) {
			if(obj.flag==0){
				 result = obj.content;
			}
			$("select[name='007004003']").append("<option value='' selected='selected'>请选择</option>");
			for (var i = 0; i < result.length; i++) {
				/* 创建option并插入数据 */
				if(trainType==result[i].id){
					$("select[name='007004003']").append("<option  selected='selected' value='" + result[i].id + "' >"
							+ result[i].name + "</option>");
				}else{
					$("select[name='007004003']").append("<option value='" + result[i].id + "' >"
							+ result[i].name + "</option>");
				}
				
			}
			var value = $("select[name='007004003']").val();
			var children = getTrainTypeChildren(value,result);
			$("select[name='007004004']")[0].options.length=0;//清空下拉内容，重新赋值
			for(var k=0;k<children.length;k++){
				$("select[name='007004004']")[0].options.add(new Option(children[k].name, children[k].id));
			}
			$("select[name='007004004']").val(trainBZ);
			$("select[name='007004003']")[0].onchange = function(){
				var value = $("select[name='007004003']").val();
				var children = getTrainTypeChildren(value,result);
				$("select[name='007004004']")[0].options.length=0;//清空下拉内容，重新赋值
				for(var k=0;k<children.length;k++){
					$("select[name='007004004']")[0].options.add(new Option(children[k].name, children[k].id));
				}
			};
		},
		error : function(error) {
			console.log(error);
		}

	});	
}
/**
 * 
 * @param a 列车类型的值
 * @param b 列车类型对象
 */
function getTrainTypeChildren(a,b){
	var children = [];
	for(var i=0;i<b.length;i++){
		var id = b[i].id;
		if(a==id){
			children = b[i].children;
		}
	}
	return children;
}
/**
 * 输入值校验
 * @param e 输入元素
 * @param type 校验类型
 */
function validate(e,type){
	if(type==1){//只能输入26个字母
		e.value=e.value.replace(/[^a-zA-Z]/g,'');
	}
}
stationList = [];//起始站信息
startStation ="";//起始站值
direction = "";//发车方向值

/**
 * 起始站输入拼音简写变化事件
 * @param e 拼音简写输入框
 */
function startStationPYChange(e){
	if(yxzzId == null){
		return;
	}
	//根据输入框的值从后台查询出相关车站信息
	$.ajax({
		url : '../../../basecase/getStationByPinYin',
		type : "POST",
		data :  {fileId: yxzzId,pinYin:e.value,type:1},
		success : function(obj) {
			console.log(obj);
			if(obj.flag==0){
				stationList = obj.content;
				$("select[name='"+e.name+"']").empty();
				if($("select[name='"+e.name+"']").length>0){
					if(stationList.length>0){
						$("select[name='"+e.name+"']").append(new Option("请选择", "请选择"));
						for (var i = 0; i < stationList.length; i++) {
							$("select[name='"+e.name+"']").append(new Option(stationList[i].name, stationList[i].stationNumber));
						}
					}
				}
			}
		}
	});	
}
/**
 * 起始站输入拼音简写变化事件
 * @param e 拼音简写输入框
 */
function stopStationPYChange(e){
	//根据输入框的值从后台查询出相关车站信息
	$.ajax({
		url : '../../../basecase/getStationByPinYin',
		type : "POST",
		data :  {fileId: yxzzId,pinYin:e.value,type:2},
		success : function(obj) {
			if(obj.flag==0){
				var endstationList = obj.content;
				$("select[name='007004014']").empty();
				if($("select[name='007004014']").length>0){
					if(endstationList.length>0){
						$("select[name='007004014']").append(new Option("请选择", "请选择"));
						for (var i = 0; i < endstationList.length; i++) {
							$("select[name='007004014']").append(new Option(endstationList[i].name, endstationList[i].stationNumber));
						}
					}
				}
			}
		}
	});	
}
/***
 * 起始站选择变化事件
 * @param e
 */
function startStationChange(e){
	//根据输入框的值从后台查询出相关车站信息
	startStation = e.value;
	for (var i = 0; i < stationList.length; i++) {
		if(startStation == stationList[i].stationNumber){
			var site = stationList[i].site;
			var directionList = stationList[i].direction;
			$("select[name='007004011']").empty();//站场名
			for(var j = 0; j < site.length; j++){
				$("select[name='007004011']").append(new Option(site[j].name, site[j].id));
			}
			$("select[name='007004011']").get(0).selectedIndex=0;//默认选中第一个
			$("select[name='007004013']").empty();
			$("select[name='007004013']").append(new Option("请选择", "请选择"));//发车方向
			for(var k = 0; k < directionList.length; k++){
				$("select[name='007004013']").append(new Option(directionList[k].name, directionList[k].id));
			}
		}
	}
}
/***
 * 发车方向变化事件
 * @param e
 */
function directionChange(e){
	console.log(e.value);
	direction = e.value;
	for (var i = 0; i < stationList.length; i++) {
		if(startStation == stationList[i].stationNumber){
			var directionList = stationList[i].direction;
			for(var j = 0; j < directionList.length; j++){
				if(direction == directionList[j].id){
					var  runningPathList = directionList[j].runningPath;
					$("select[name='007004015']").empty();
					$("select[name='007004015']").append(new Option("无", "0"));//运行径路选择
					for(var k = 0; k < runningPathList.length; k++){
						$("select[name='007004015']").append(new Option(runningPathList[k].name, runningPathList[k].id));
					}
				}
				}
		}
	}
}


/***针对测试序列的事件
 * 起始站选择变化事件
 * @param e
 */
function startStationChange2(e){
	//根据输入框的值从后台查询出相关车站信息
	startStation = e.value;
	for (var i = 0; i < stationList.length; i++) {
		if(startStation == stationList[i].stationNumber){
			var site = stationList[i].site;
			var directionList = stationList[i].direction;
			$("select[name='007004013']").empty();
			$("select[name='007004013']").append(new Option("请选择", "请选择"));//发车方向
			for(var k = 0; k < directionList.length; k++){
				$("select[name='007004013']").append(new Option(directionList[k].name, directionList[k].id));
			}
		}
	}
}
/***针对测试序列的事件
 * 发车方向变化事件
 * @param e
 */
function directionChange2(e) {
	direction = e.value;
	for (var i = 0; i < stationList.length; i++) {
		if (startStation == stationList[i].stationNumber) {
			var directionList = stationList[i].direction;
			for (var j = 0; j < directionList.length; j++) {
				if (direction == directionList[j].id) {
					var runningPathList = directionList[j].runningPath;
//					$("select[name='007004015']").empty();
//					$("select[name='007004015']").append(new Option("无", "0"));// 运行径路选择
//					for (var k = 0; k < runningPathList.length; k++) {
//						$("select[name='007004015']").append(
//								new Option(runningPathList[k].name,
//										runningPathList[k].id));
//					}
				}
			}
		}
	}
}

//根据type获取lkj换装文件数据，软件类型和文件名称的切换联动
function getLKJByType(type,selectTargetData){
	$.ajax({
		url : "../../../LKJSoftwareVersion/getVersionName",
		type : "post",
		data : {
			"sId" : 1,
			"type" : type
		},
		dataType : "json",
		async:false,
		success : function(data) {
			if(data.flag==0){
				console.log(data);
				var content = data.content;
				$("select[name='007016001']").find("option").remove();
				for(var i = 0;i<content.length;i++){
					$("select[name='007016001']").append("<option value='" + content[i].VERSION_ID + "' >"
							+ content[i].VERSION_NAME + "</option>");
				}
				storeVersionByType = content;
				if($("select[name='007016001']").length>0){
					$("select[name='007016001']").get(0).selectedIndex = 0;
					$("select[name='007016001']").trigger("change");
				}
				
			}
		},
		error : function() {
		}
	});
}